package com.mc.main;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import message.UdpMessage;

import android.util.Log;

/**
 * 
 * Dummy to get some values ...
 * 
 * @Author Mikael Bertlin, Sajox
 *
 */
public class UnitAccessDummy implements UnitAccess, UdpEventListener{

	private static final String FAN = "fan";
	private static final String TEMP = "temp";
	private static final String LED = "led";
	private static final String SET_COMMAND = "set";
	private static final String GET_COMMAND  = "get";
	
	private UdpClient client;
	private int fanSpeed;
	private int temp;
	private int led;
	int seed = 12357;

	public UnitAccessDummy(UdpClient client){
		this.client = client;
	}
	
	public int getFan() {
		//send get message and return current value, we send it asynch.
		client.send(new UdpMessage(GET_COMMAND, "fan", 0).ASCIISerialize());
		return fanSpeed;
	}

	 public int getTemp() {
		client.send(new UdpMessage(GET_COMMAND, "temp", 0).ASCIISerialize());
		//TODO: remove and get real!
		//Random generator = new Random(seed);
		//int temp = generator.nextInt(200);
		//seed += 3;
		return temp;
	}
	
	public int getLed() {
		client.send(new UdpMessage(GET_COMMAND, "led", 0).ASCIISerialize());
		return led;
	}

	 public int[] getTempAndFan() {
		return new int[]{getTemp(), getFan()};
	}

	public void setFan(int speed) {
		client.send(new UdpMessage(SET_COMMAND, "fan", speed).ASCIISerialize());
	}
	
	public void setTemp(int temperature) {
		client.send(new UdpMessage(SET_COMMAND, "temp", temperature).ASCIISerialize());
	}
	
	public void setLed(int value) {
		client.send(new UdpMessage(SET_COMMAND, "led", value).ASCIISerialize());	
	}
	
	private byte[] serializeObject(UdpMessage m){
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream out;
		
		try {
			out = new ObjectOutputStream(bos);
			out.writeObject(m);
	        out.close();
		} catch (IOException e) {
			Log.v("UnitAccessDummy", "serializeObject exception");
			return null;
		}
		
		return bos.toByteArray();
	}

	/* (non-Javadoc)
	 * @see com.mc.main.UdpEventListener#UdpEventReceived(com.mc.main.UdpEvent)
	 */
	public void UdpEventReceived(UdpEvent evt) {
		Log.v("UnitAccessDummy", "UdpEventReceived");
		UdpMessage um = null;
		String type = "";
		try {
			um = new UdpMessage("","",0);
			um.ASCIIDeSerialize(evt.getData(), evt.getData().length);

			type = um.getType();
			Log.v("UnitAccessDummy", "Type received: " + type + " value: " + um.getValue());
		}catch (Exception e) {}
		
		
		if(type.equals(FAN)){
        	fanSpeed = um.getValue();
        }else if(type.equals(TEMP)){
        	temp = um.getValue();
        }else if(type.equals(LED)){
        	led = um.getValue();
        }

	}
}
